home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14416 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.5 KB  |  59 lines

  1. Newsgroups: comp.lang.c
  2. Path: new-news.sprintlink.net!eskimo!scs
  3. From: scs@eskimo.com (Steve Summit)
  4. Subject: Re: How to detect EOF?/filepointer problem.
  5. X-Nntp-Posting-Host: eskimo.com
  6. Message-ID: <Dpv413.LtC@eskimo.com>
  7. Sender: news@eskimo.com (News User Id)
  8. Organization: schmorganization
  9. References: <316F8932.2BAF@www.partio.fi>
  10. Date: Sun, 14 Apr 1996 17:17:26 GMT
  11.  
  12. In article <316F8932.2BAF@www.partio.fi>, "R.K.Brand"
  13. <ralph@www.partio.fi> writes:
  14. > ...I have a problem while scanning through a file too see
  15. > whether or not a certain word occurs.  When I run out of file,
  16. > fscanf() keeps returning the last line, instead of some NULL value...
  17. > How can I test for the end of the file?
  18.  
  19. fscanf() never returns pointers, so it can't return NULL.
  20. It will return EOF at end-of-file, as long as nothing weird is
  21. going on.  But weird things do go on with scanf.  If you're
  22. reading lines, a line-based input function such as fscanf is
  23. usually a much better choice.
  24.  
  25. (If you're reading words using fscanf and %s, and it has reached
  26. end-of-file, it's likely that your word buffer will still contain
  27. a copy of the last word read, regardless of whether fscanf tried
  28. to tell you it reached EOF and regardless of whether you noticed.)
  29.  
  30. You didn't mention feof(), but if you happen to be using it, make
  31. sure you call it *after* calling an input function that might
  32. have detected EOF, not before.  (C's I/O is not like Pascal's.
  33. feof() does not detect EOF; it merely reports whether some input
  34. function has detected it.)
  35.  
  36. > The second problem is how to manipulate a pointer of type
  37. > "FILE"..so, a filepointer.  At a certain point I want to skip
  38. > 11 chars back in the file and start writing.  string-=11; gives
  39. > me an error and after string--; fprintf(string,"foo"); does not
  40. > do anything anymore.
  41.  
  42. FILE pointers are not strings, they're, well, file pointers.
  43. In fact, you can't manipulate them at all (they're supposed to be
  44. an "opaque" type); everything you do with a FILE pointer, you do
  45. by passing that FILE pointer to a library function (or macro).
  46. To move around in the stream, use fseek().  Beware, though, that
  47. it's not portable to use fseek() to "skip 11 characters back" in
  48. a text file; strictly speaking, fseek() on text files may only be
  49. used to seek to the beginning or end of the file or to a position
  50. returned by a prior call to ftell().
  51.  
  52. > I you respond to this one, please do also mail me a copy.
  53.  
  54. Tell you what: how about I send you mail telling you you can find
  55. a copy here?  (Or is your news feed extremely unreliable?)
  56.  
  57.                     Steve Summit
  58.                     scs@eskimo.com
  59.